package com.sense.kukoo.biz_media.view

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.sense.kukoo.biz_media.R
import com.sense.kukoo.biz_media.databinding.BizMediaActivityMediaListBinding
import com.sense.kukoo.biz_media.vm.MediaListViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.media.Kukoo
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.bean.*
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.biz_media.adapter.MediaAdapter
import com.sense.kukoo.common.anim.BottomMotionAnimHelper
import com.sense.kukoo.common.wiget.dialog.MiddleConfigDialog
import com.sense.kukoo.common.wiget.state.EmptyMediaListState
import com.zy.multistatepage.state.SuccessState

@Route(path = ARouterUrl.ROUTE_MEDIA_MODULE_MEDIA_LIST_ACTIVITY)
class MediaListActivity : BaseMvvmActivity<MediaListViewModel, BizMediaActivityMediaListBinding>() {

    private val TAG: String = javaClass.simpleName
    private val mAnimHelper = BottomMotionAnimHelper()

    private var state = STATE_SUCCESS
    private var mPlayState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING
    private var mCurrentMusicState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING
    private var mCurrentMusicProgress: Int = 1

    private lateinit var mMediaAdapter: MediaAdapter

    companion object {
        val STATE_SUCCESS = 0
        val STATE_EMPTY = 1
    }

    override fun getLayoutId(): Int = R.layout.biz_media_activity_media_list

    override fun getViewModelClass(): Class<MediaListViewModel> = MediaListViewModel::class.java

    override fun responseVM(action: Int) {}

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .navigationBarColor(com.sense.kukoo.common_design.R.color.bg_background)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .fitsSystemWindows(false)
            .init()
    }

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        bindAction(mViewModel.action_back){
            mAnimHelper.animateToFinish()
        }
        bindAction(mViewModel.action_showCleanAllDialog){
            showCleanAllDialog()
        }
        //
        bindData(mViewModel.data_playListData){
            if(it.size == 0){
                setState(MediaListActivity.STATE_EMPTY)
            } else {
                setState(MediaListActivity.STATE_SUCCESS)
                mMediaAdapter.list = it
                mMediaAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        //init
        mMediaAdapter = MediaAdapter(arrayListOf(), mMediaListener)
        mBinding.recyclerView.adapter = mMediaAdapter
        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        //load data
        mViewModel.refreshPlayListData()

        //弹出动画
        val first = ApplicationContext.dp2px(800F).toFloat()
        mAnimHelper.init(mBinding.flMediaListRoot, mBinding.clMediaListContent, first){ finish() }
        mAnimHelper.animateToStart()
    }

    override fun onBackPressed() {
        mAnimHelper.animateToFinish()
    }

    private fun showCleanAllDialog() {
        val dialog = MiddleConfigDialog(this)
        val mListener = object : MiddleConfigDialog.MiddleConfigListener {
            override fun onLeftClick() {
                dialog.dismiss()
            }

            override fun onRightClick() {
                dialog.dismiss()
                Kukoo.input.deleteAll()
            }
        }
        dialog
            .setTitleStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.media_controler_clean_title))
            .setMsgStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.media_controler_clean_hint))
            .setBtnLeft(
                    ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_left_btn),
                    this.getResources()
                        .getColor(com.sense.kukoo.common_design.R.color.text_active)
            )
            .setBtnRight(
                    ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_right_btn),
                    this.getResources()
                        .getColor(com.sense.kukoo.common_design.R.color.fg_error)
            )
            .setListener(mListener)
            .show()
    }

    private fun setState(s: Int) {
        if (state != s) {
            state = s
            when (state) {
                MediaListActivity.STATE_SUCCESS -> {
                    mBinding.multiStateContainer.show<SuccessState>()
                    mBinding.tvBtnCleanAll.visibility = View.VISIBLE
                }
                MediaListActivity.STATE_EMPTY -> {
                    mBinding.multiStateContainer.show<EmptyMediaListState>()
                    mBinding.tvBtnCleanAll.visibility = View.GONE
                }
            }
        }
    }

    private val mWatcher = object: UserWatcher.MusicStateWatcher{
        override fun onMusicChanged(music: LocalMediaListItemBean) {
            mMediaAdapter.setCurrentMusic(music)
        }

        override fun onPlayTimeChanged(currentInt: Int, totalInt: Int, current: String, total: String) {}

        override fun onPlayPercentChanged(percent: Int) {
            if(mCurrentMusicProgress != percent){
                mCurrentMusicProgress = percent
                mMediaAdapter.setCurrentPlayState(mCurrentMusicState, mCurrentMusicProgress)
            }
        }

        override fun onPlayStateChanged(state: Int) {
            mPlayState = state
            if(mCurrentMusicState != state){
                mCurrentMusicState = state
                mMediaAdapter.setCurrentPlayState(mCurrentMusicState, mCurrentMusicProgress)
            }
        }

        override fun onPlaySpeedChanged(speed: Float) {}

        override fun onPlayListChanged() {
            //播放列表变化
            mViewModel.refreshPlayListData()
        }
    }

    val mMediaListener = object : MediaAdapter.MediaListener {
        override fun onItemClick(bean: MediaItemBean, position: Int) {
            val data: LocalMediaListItemBean = bean.data
        }

        override fun onMediaPlayClick(bean: MediaItemBean, position: Int) {
            val data:LocalMediaListItemBean = bean.data
            val currentPlayMusic = Kukoo.play.getCurrentPlayMusic()
            if(currentPlayMusic?.idOfChapter() == data.idOfChapter()){
                //当前的
                when (mPlayState) {
                    UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                    UserWatcher.MusicStateWatcher.STATE_SEEKING,
                    UserWatcher.MusicStateWatcher.STATE_PREPARE,
                    UserWatcher.MusicStateWatcher.STATE_PLAYING -> {
                        Kukoo.input.pause()
                    }
                    /*
                    UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING ->{}
                    UserWatcher.MusicStateWatcher.STATE_PAUSE ->{}
                    */
                    else -> {
                        Kukoo.input.start()
                    }
                }
            }else{
                //非当前的
                Kukoo.input.play(data)
            }
        }

        override fun onMediaToList(bean: MediaItemBean, position: Int) {}

        override fun onMediaDelete(bean: MediaItemBean, position: Int) {
            Kukoo.input.delete(bean.data)
        }

        override fun onTouchChanged(isTouching: Boolean) {}
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Kukoo.watcher.register(mWatcher)
    }

    override fun onDestroy() {
        super.onDestroy()
        Kukoo.watcher.unregister(mWatcher)
    }
}